package com.defence.costomapp.activity.statistics;

import android.content.Context;
import android.content.Intent;
import android.os.Bundle;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.BaseAdapter;
import android.widget.LinearLayout;
import android.widget.ListView;
import android.widget.TextView;

import com.blankj.utilcode.util.SPUtils;
import com.defence.costomapp.R;
import com.defence.costomapp.base.BaseActivity;
import com.defence.costomapp.db.LineDataBase;
import com.defence.costomapp.db.LineHistoryModel;
import com.defence.costomapp.myinterface.RVItemClickListener;
import com.defence.costomapp.net.Constant;
import com.raizlabs.android.dbflow.sql.language.Case;
import com.raizlabs.android.dbflow.sql.language.Delete;
import com.raizlabs.android.dbflow.sql.language.SQLite;

import java.util.Collections;
import java.util.List;

import butterknife.BindView;
import butterknife.ButterKnife;

public class LineHistoryActivity extends BaseActivity implements View.OnClickListener {

    @BindView(R.id.back)
    TextView back;
    @BindView(R.id.middle_title)
    TextView middleTitle;
    @BindView(R.id.right_title)
    TextView rightTitle;
    @BindView(R.id.lv_historyRecord)
    ListView lvHistoryRecord;
    private LineHistoryAdapter mLineHistoryAdapter;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_line_history);
        ButterKnife.bind(this);
        initView();
    }

    private void initView() {
        back.setText("返回");
        back.setTextColor(getResources().getColor(R.color.bule_light));
        middleTitle.setText("历史记录");
        middleTitle.setTextColor(getResources().getColor(R.color.bule_light));
        rightTitle.setText("清除历史");
        rightTitle.setTextColor(getResources().getColor(R.color.bule_light));
        back.setOnClickListener(this::onClick);
        rightTitle.setOnClickListener(this::onClick);

//        List<LineHistoryModel> lineHistoryModels = SQLite.select().from(LineHistoryModel.class).queryList();
//       去重后的查询
        List<LineHistoryModel> lineHistoryModels = SQLite.select().distinct().from(LineHistoryModel.class).queryList();
        //        倒叙排行
        Collections.reverse(lineHistoryModels);
        mLineHistoryAdapter = new LineHistoryAdapter(this, lineHistoryModels, new RVItemClickListener() {
            @Override
            public void onItemClick(int position) {

                //左轴
                SPUtils.getInstance(Constant.SHARED_NAME).put(Constant.DATA_STYPEID, lineHistoryModels.get(position).getStypeId());

                SPUtils.getInstance(Constant.SHARED_NAME).put(Constant.MACHINE_NUMBERS, lineHistoryModels.get(position).getMachineNumbers());
                SPUtils.getInstance(Constant.SHARED_NAME).put(Constant.GUIGEIDS, lineHistoryModels.get(position).getGuigeids());
                SPUtils.getInstance(Constant.SHARED_NAME).put(Constant.ISZHENGZHANGLV, lineHistoryModels.get(position).getIszhengzhanglv());


                SPUtils.getInstance(Constant.SHARED_NAME).put(Constant.MACHINE_NUMBERSS, lineHistoryModels.get(position).getMachineNumbers2());
                SPUtils.getInstance(Constant.SHARED_NAME).put(Constant.GUIGEIDSS, lineHistoryModels.get(position).getGuigeids2());
                SPUtils.getInstance(Constant.SHARED_NAME).put(Constant.ISZHENGZHANGLVS, lineHistoryModels.get(position).getIszhengzhanglv2());


                //     右轴
                SPUtils.getInstance(Constant.SHARED_NAME).put(Constant.DATA_STYPEIDRIGHT, lineHistoryModels.get(position).getStypeId3());

                SPUtils.getInstance(Constant.SHARED_NAME).put(Constant.MACHINENUMBERSRIGHT, lineHistoryModels.get(position).getMachineNumbers3());
                SPUtils.getInstance(Constant.SHARED_NAME).put(Constant.GUIGEIDSRIGHT, lineHistoryModels.get(position).getGuigeids3());
                SPUtils.getInstance(Constant.SHARED_NAME).put(Constant.ISZHENGZHANGLVRIGHT, lineHistoryModels.get(position).getIszhengzhanglv3());


                SPUtils.getInstance(Constant.SHARED_NAME).put(Constant.MACHINENUMBERSRIGHTS, lineHistoryModels.get(position).getMachineNumbers4());
                SPUtils.getInstance(Constant.SHARED_NAME).put(Constant.GUIGEIDSRIGHTS, lineHistoryModels.get(position).getGuigeids4());
                SPUtils.getInstance(Constant.SHARED_NAME).put(Constant.ISZHENGZHANGLVRIGHTS, lineHistoryModels.get(position).getIszhengzhanglv4());

                SPUtils.getInstance(Constant.SHARED_NAME).put(Constant.CTYPECYLEFT, lineHistoryModels.get(position).getMctypecyleft());
                SPUtils.getInstance(Constant.SHARED_NAME).put(Constant.CTYPECYRIGHT, lineHistoryModels.get(position).getMctypecyright());
                SPUtils.getInstance(Constant.SHARED_NAME).put(Constant.SDATE, lineHistoryModels.get(position).getMsdate());

                Intent intent = new Intent(LineHistoryActivity.this, MpLineChartActivity.class);
                startActivity(intent);

            }
        });
        lvHistoryRecord.setAdapter(mLineHistoryAdapter);
    }

    @Override
    public void onClick(View v) {
        switch (v.getId()) {
            case R.id.back:
                finish();
                break;
            case R.id.right_title:
//                如果删除整个表，可以这样：
                Delete.table(LineHistoryModel.class);
                List<LineHistoryModel> lineHistoryModels = SQLite.select().from(LineHistoryModel.class).queryList();
                mLineHistoryAdapter = new LineHistoryAdapter(this, lineHistoryModels, new RVItemClickListener() {
                    @Override
                    public void onItemClick(int position) {
                    }
                });
                lvHistoryRecord.setAdapter(mLineHistoryAdapter);
                break;
        }
    }

    public class LineHistoryAdapter extends BaseAdapter {

        private Context context;
        private LayoutInflater inflater;
        private List<LineHistoryModel> list;
        private RVItemClickListener rvItemClickListener;

        public LineHistoryAdapter(Context context, List<LineHistoryModel> list, RVItemClickListener rvItemClickListener) {
            super();
            this.context = context;
            inflater = LayoutInflater.from(context);
            this.list = list;
            this.rvItemClickListener = rvItemClickListener;
        }

        @Override
        public int getCount() {
            // TODO Auto-generated method stub
            if (list != null && list.size() > 0) {
                return list.size();
            }
            return 0;
        }

        @Override
        public Object getItem(int position) {
            return list.get(position);
        }

        @Override
        public long getItemId(int position) {
            return position;
        }

        @Override
        public View getView(int position, View convertView, ViewGroup parent) {
            if (convertView == null) {
                convertView = inflater.inflate(R.layout.item_historydata, null);
            }
            TextView tv_leftaxis = convertView.findViewById(R.id.tv_leftaxis);
            TextView tv_rightaxis = convertView.findViewById(R.id.tv_rightaxis);
            LinearLayout liearitemll = convertView.findViewById(R.id.liearitemll);

            LineHistoryModel lineHistoryModel = list.get(position);

            tv_leftaxis.setText("左轴:" + lineHistoryModel.getFiltername() + " - " + lineHistoryModel.getMachinename() + lineHistoryModel.getMachinename2());
            tv_rightaxis.setText("右轴:" + lineHistoryModel.getFiltername2() + " - " + lineHistoryModel.getMachinename3() + lineHistoryModel.getMachinename4());

            liearitemll.setOnClickListener(new View.OnClickListener() {
                @Override
                public void onClick(View view) {
                    rvItemClickListener.onItemClick(position);
                }
            });
            return convertView;
        }
    }
}
